package com.cykj.management.activity;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.nfc.Tag;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Window;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;

import com.cykj.management.common.MyLog;
import com.iflytek.cloud.VoiceWakeuper;

import java.util.Map;

import io.flutter.embedding.android.FlutterActivity;
import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.plugin.common.BasicMessageChannel;
import io.flutter.plugin.common.StandardMessageCodec;
import io.flutter.plugins.GeneratedPluginRegistrant;

/**
 * @author haojie
 * @date 2021/06/18 08:49
 */

public abstract class BaseActivity extends FlutterActivity implements BasicMessageChannel.MessageHandler {
    private static final String TAG = BaseActivity.class.getSimpleName();
    public static final String MscChannelName = "MscChannel";


    public static BasicMessageChannel mscChannel;
    /**
     * 闸门名称数据
     */
    public static String gateNameData;

    private Context mContext;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
    }

    private static String[] PERMISSIONS = {
            Manifest.permission.RECORD_AUDIO
    };

    private Handler handler = new Handler() {

        @Override
        public void handleMessage(@NonNull Message msg) {
            switch (msg.what) {
                case 1:
                    WeakActivity.getInstance().init(mContext);
                    break;
                case 2:
                    WeakActivity.getInstance().release();
                    break;
            }
        }
    };

    @Override
    public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
        GeneratedPluginRegistrant.registerWith(flutterEngine);
        mscChannel = new BasicMessageChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), BaseActivity.MscChannelName, new StandardMessageCodec());
        mscChannel.setMessageHandler(this);
        mContext = getApplicationContext();
        int record_audio_permission = ActivityCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO);
        if (record_audio_permission != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, PERMISSIONS, 1);
        }

        handler.sendEmptyMessageDelayed(1, 1000 * 1);
        handler.sendEmptyMessageDelayed(2, 1000 * 5);
//        weakActivity.release();
    }

    @Override
    public void onMessage(@Nullable final Object message, @NonNull final BasicMessageChannel.Reply reply) {
        MyLog.i(BaseActivity.TAG, "onMessage: " + (message != null ? message.toString() : null));
        Map map = (Map) message;
        Map msg = (Map) map.get("message");
        String cmd = (String) msg.get("cmd");

        if (cmd.equals("init")) {
            MyLog.d(TAG, "init");
            gateNameData = (String) msg.get("data");
            WeakActivity.getInstance().init(mContext);
            AsrActivity asrActivity = AsrActivity.getInstance();
            asrActivity.init(mContext, gateNameData, mscChannel);
        } else if ("listen".equals(cmd)) {
            MyLog.d(TAG, "listen");
            // 初始化合成对象
//            AsrActivity asrActivity = AsrActivity.getInstance();
//            asrActivity.init(mContext, gateNameData, mscChannel);
//            asrActivity.startRecognize();
//            OneShotActivity oneShotActivity = OneShotActivity.getInstance();
//            oneShotActivity.init(this);
//            oneShotActivity.oneshot();
        } else if ("speak".equals(cmd)) {
            TtsActivity ttsActivity = TtsActivity.getInstance();
            ttsActivity.init(mContext);
            Map data = (Map) msg.get("data");
            MyLog.d(TAG, "speak: " + data.toString());
            boolean isOpen = (Boolean) data.get("openGateResult");
            if (isOpen) {
                ttsActivity.tts_play("道闸已开启");
            } else {
                String failMessage = (String) data.get("failMessage");
                MyLog.e(TAG, failMessage);
                ttsActivity.tts_play(failMessage);
            }
        } else if ("dispose".equals(cmd)) {
            WeakActivity.getInstance().release();
            AsrActivity.getInstance().release();
            TtsActivity.getInstance().release();
        }
    }
}
